package com.ford.trade.processor;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.SneakyThrows;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: ford
 * @date: 2022/4/23 13:41
 */
@Data
@Component
public class PictureProcessor implements PageProcessor {

    @SneakyThrows
    @Override
    public void process(Page page) {
        Document document = page.getHtml().getDocument();
        Elements select = document.getElementById("thumbs").select("figure");
        List<String> urls = new ArrayList<>();
        for (Element element : select) {
            String src = element.getElementsByClass("preview").attr("href");
            String id = element.attr("data-wallpaper-id");
            if ("".equals(src)){
                continue;
            }
            System.out.println(src);
            HttpClient httpclient = new DefaultHttpClient(new PoolingClientConnectionManager());
            HttpUriRequest httpUriRequest = new HttpGet(src);
            httpUriRequest.setHeader("referer","https://wallhaven.cc/user/Calm/favorites/623697?purity=110&page=1");
            httpUriRequest.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            httpUriRequest.setHeader("cookie","_pk_id.1.01b8=9d9a645dfd949fb8.1655455794.; _pk_ses.1.01b8=1; XSRF-TOKEN=eyJpdiI6Inc5Z3FENUZYbm1tMVhoK0E3dzdrcGc9PSIsInZhbHVlIjoiQ1VPM2RmaU9DSDB0SklpTjJoTWJPdWZwQ3VcL3RaZFBycGkwRlNGaUpIcUVXdFVKZmtmWVhRYitad3RMMzVuRmciLCJtYWMiOiJlZmQ0NGE1MzdjMjRkYjdhYmY4NmRkODQ5M2NkMTIzYmEyOWRmMTU3NzgwMjE2MDgxMmEzNTViNDU4NGUyY2Q3In0%3D; wallhaven_session=eyJpdiI6IkFlSjV1aGtOcCtQZDIrYmVoalNmbHc9PSIsInZhbHVlIjoiZEZ2MWp4ZEJtR1pkd1wveXNcL2U1Tkk4QTA5NjRSbGk2SXNNUmxrVSt4UFpHZndmZDdzZHdjOEdhUVJTMjNPbGdnIiwibWFjIjoiZWFmNzc4MWJmNWM5YzY1MmJmZTFkM2IwMDY0YjhjZmNmYTRiMDJmNDk3NzZlZjBkZDY1OWFhMGFmMzcxZmQ1ZiJ9");
            HttpResponse response = null;
            response = httpclient.execute(httpUriRequest);
            HttpEntity entity = response.getEntity();
            InputStream inStream = entity.getContent();
            byte[] data = readInputStream(inStream);
            //new一个文件对象用来保存图片，默认保存当前工程根目录
            File imageFile = new File("E:/Downloads/imgs/" + id + ".png");
            //创建输出流
            FileOutputStream outStream = new FileOutputStream(imageFile);
            //写入数据
            outStream.write(data);
            //关闭输出流
            outStream.close();

//            //获取输入流
//            URL target = new URL(src);
//            HttpURLConnection connection = (HttpURLConnection) target.openConnection();
//            String ref = target.getRef();
//            connection.setRequestMethod("GET");
//            connection.setRequestProperty("referer","https://wallhaven.cc/user/Calm/favorites/623697?purity=110&page=1");
//            connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//            connection.setRequestProperty("cookie","_pk_id.1.01b8=9d9a645dfd949fb8.1655455794.; _pk_ses.1.01b8=1; XSRF-TOKEN=eyJpdiI6Inc5Z3FENUZYbm1tMVhoK0E3dzdrcGc9PSIsInZhbHVlIjoiQ1VPM2RmaU9DSDB0SklpTjJoTWJPdWZwQ3VcL3RaZFBycGkwRlNGaUpIcUVXdFVKZmtmWVhRYitad3RMMzVuRmciLCJtYWMiOiJlZmQ0NGE1MzdjMjRkYjdhYmY4NmRkODQ5M2NkMTIzYmEyOWRmMTU3NzgwMjE2MDgxMmEzNTViNDU4NGUyY2Q3In0%3D; wallhaven_session=eyJpdiI6IkFlSjV1aGtOcCtQZDIrYmVoalNmbHc9PSIsInZhbHVlIjoiZEZ2MWp4ZEJtR1pkd1wveXNcL2U1Tkk4QTA5NjRSbGk2SXNNUmxrVSt4UFpHZndmZDdzZHdjOEdhUVJTMjNPbGdnIiwibWFjIjoiZWFmNzc4MWJmNWM5YzY1MmJmZTFkM2IwMDY0YjhjZmNmYTRiMDJmNDk3NzZlZjBkZDY1OWFhMGFmMzcxZmQ1ZiJ9");
//            connection.connect();
//            System.out.println(target);
//            InputStream inputStream = connection.getInputStream();
//            OutputStream outputStream = new FileOutputStream("E:/Downloads/imgs/" + id + ".png");
//            int tmp = 0;
//            while ((tmp = inputStream.read()) != -1) {
//                outputStream.write(tmp);
//            }
//            System.out.println(id + "下载完毕");
//            inputStream.close();
//            outputStream.close();

//            Thread.sleep(1000);
//            ImageUtil.saveImage(src,"E:/Downloads/imgs/" + id + ".jpg");
            urls.add(src);
        }
        System.out.println(JSON.toJSONString(urls));
//        ImageUtil.saveImages(urls,);
    }

    public static byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while( (len=inStream.read(buffer)) != -1 ){
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }

    private Site site = Site.me()
            .setCharset("gb2312")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
            .addCookie("Cookie", "_pk_id.1.01b8=9d9a645dfd949fb8.1655455794.; _pk_ses.1.01b8=1; XSRF-TOKEN=eyJpdiI6ImpCSkVsRThVc3dLeGhPallielJoY2c9PSIsInZhbHVlIjoiQlVQZnIrR1JmSVJjbFM4dkhHVlhRaTZRQytCdFNYQ1VQT1dpOGExS0NYM1ZlMlJRekZCZnpOaHkxVVE5SFozYiIsIm1hYyI6IjA2MjdmYjA1MTYzYzcxMGY0MzJkODczYTdjMGY3MmUzYzI4MjUxZmNlNjFiOWM5ZjMxODc5NWU3ZDE1OTVmYmYifQ%3D%3D; wallhaven_session=eyJpdiI6Iithekh2S3pQT1VsQUpqTDZHMFlUUnc9PSIsInZhbHVlIjoiRWZrVGRrNEpXOGk2ZTlTeTJXd1hQdUpMRWZ3dlFrY0VueVl3K3U1Yk5EXC9HNStzYWFOVlM4bGpZeXh1Mkg2YWgiLCJtYWMiOiJlNjNmMjAxMzk3ZTA5YTQ1YWFjMzRkY2MzNTMwYWNjMWUwYWJjNmQ0NGM1NzlhZGVkZWMxOGY4Mzk5MTUxNTA3In0%3D")
            .setTimeOut(1000000)
            .setRetrySleepTime(10000)
            .setRetryTimes(3);

    @Override
    public Site getSite() {
        return site;
    }

}
